//
//  MPHomeViewController.m
//  sanguosha
//
//  Created by caiqilin on 2017/6/30.
//  Copyright © 2017年 caiqilin. All rights reserved.
//

#import "MPHomeViewController.h"
#import "CollectionNormalCell.h"
#import "RecommendCycleView.h"
#import "CollectionHeaderView.h"
#import "MPHomeDetailViewController.h"
#import "MPWebViewController.h"
@interface MPHomeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,RecommendCycleViewDelegate>
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *homeDetailArr;
//@property (nonatomic, strong) RecommendCycleView *cycleView;

@end

@implementation MPHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    [self setupCollectionView];
    [self initData];
}

- (void)initData{
    _homeDetailArr = [NSMutableArray arrayWithCapacity:0];
    
    NSString *path=[[NSBundle mainBundle] pathForResource:@"HomeDetail" ofType:@"plist"];
    _homeDetailArr=[NSArray arrayWithContentsOfFile:path];
}
- (void)setupCollectionView {
    self.navigationItem.title = @"首页";
    RecommendCycleView *cycleView = [RecommendCycleView recommendCycleView];
    cycleView.delegate = self;
    cycleView.frame = CGRectMake(0, - SCREEN_WIDTH * 4 / 8 , SCREEN_WIDTH, SCREEN_WIDTH * 4 / 8);

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    CGFloat kWidth = (SCREEN_WIDTH - 3 * 10) / 2;
    CGFloat kHeight = kWidth * 3 / 4;
    flow.itemSize = CGSizeMake(kWidth, kHeight);
    flow.minimumLineSpacing = 5;
    flow.minimumInteritemSpacing = 5;
    flow.sectionInset = UIEdgeInsetsMake(10, 5, 0, 5);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
    self.collectionView = collectionView;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [collectionView registerNib:[UINib nibWithNibName:@"CollectionNormalCell" bundle:nil] forCellWithReuseIdentifier:@"kNormalCellID"];
    [collectionView addSubview:cycleView];
    collectionView.contentInset = UIEdgeInsetsMake(SCREEN_WIDTH * 4 / 8, 0, 0, 0);
    [self.view addSubview:collectionView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionNormalCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kNormalCellID" forIndexPath:indexPath];
    cell.iconImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"data_%ld.jpg",indexPath.item + 1]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat kNormalItemW = (SCREEN_WIDTH - 3 * 5) / 2;

    return CGSizeMake(kNormalItemW, kNormalItemW * 3 / 4);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MPHomeDetailViewController *detail = [[MPHomeDetailViewController alloc] init];
    detail.homeDetailArr = _homeDetailArr[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
}

//- (void)recommendCycleViewClick:(RecommendCycleView *)recommendCycleView :(NSString *)pid {
//    MPWebViewController *web = [[MPWebViewController alloc] init];
//    web.pid = pid;
//    [self.navigationController pushViewController:web animated:YES];
//}

- (void)recommendCycleViewClick:(RecommendCycleView *)recommendCycleView :(BmobObject *)obj {
    MPWebViewController *web = [[MPWebViewController alloc] init];
    web.obj = obj;
    [self.navigationController pushViewController:web animated:YES];
}

@end

